package com.biwin.module.org.starter.controller;

import com.biwin.common.app.basic.BwQueryDto;
import com.biwin.common.app.exception.BwBasicException;
import com.biwin.common.app.exception.BwNotFoundException;
import com.biwin.core.main.controller.BwBasicController;
import com.biwin.module.org.api.dto.SystemUserDto;
import com.biwin.module.org.api.enums.BwSystemUserExceptionEnum;
import com.biwin.module.org.api.service.ISystemUserService;
import com.biwin.module.org.api.vo.SystemUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * System User Controller
 *
 * @author biwin
 * @since 0.0.1
 */
@Api(tags = "用户管理")
@RestController("System User Controller")
@RequestMapping("/api/auth/system/user")
public class SystemUserController extends BwBasicController {
    private static final Logger log = LoggerFactory.getLogger(SystemUserController.class);
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @ApiOperation(value = "用户列表", httpMethod = "POST")
    @PostMapping("/list")
    public void queryList(@RequestBody @Validated BwQueryDto queryDto) {}

    @ApiOperation(value = "添加用户", httpMethod = "PUT")
    @PutMapping("/add")
    public SystemUserVo add(@RequestBody @Validated SystemUserDto systemUserDto) {
        try {
            systemUserService.findByAccount(systemUserDto.getAccount());
        } catch (BwNotFoundException e) {
            systemUserDto.setPassword(passwordEncoder.encode(systemUserDto.getPassword()));
            return systemUserService.add(systemUserDto);
        }
        throw new BwBasicException(BwSystemUserExceptionEnum.USER_EXIST);
    }

    @ApiOperation(value = "修改用户", httpMethod = "POST")
    @PostMapping("/update")
    public SystemUserVo update(@RequestBody @Validated SystemUserDto systemUserDto) {
        Assert.notNull(systemUserDto.getId(), "id must be not null");
        Assert.notNull(systemUserDto.getVersion(), "version must be not null");
        systemUserDto.setPassword(passwordEncoder.encode(systemUserDto.getPassword()));
        return systemUserService.update(systemUserDto);
    }

    @ApiOperation(value = "删除用户", httpMethod = "DELETE")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable(name = "id") Long id) {
        systemUserService.delete(id);
    }

    @ApiOperation(value = "锁定用户", httpMethod = "POST")
    @PostMapping("/lock/{id}")
    public void lock(@PathVariable(name = "id") Long id) {
        systemUserService.lock(id);
    }

    @ApiOperation(value = "解锁用户", httpMethod = "POST")
    @PostMapping("/unlock/{id}")
    public void unLock(@PathVariable(name = "id") Long id) {
        systemUserService.unlock(id);
    }

    @ApiOperation(value = "禁用用户", httpMethod = "POST")
    @PostMapping("/disable/{id}")
    public void disable(@PathVariable(name = "id") Long id) {
        systemUserService.disable(id);
    }

    @ApiOperation(value = "启用用户", httpMethod = "POST")
    @PostMapping("/enable/{id}")
    public void enable(@PathVariable(name = "id") Long id) {
        systemUserService.enable(id);
    }

}
